package com.example.catpict;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class GetFromGallery extends Activity {
	
	private static final int SELECT_PICTURE = 1;
	Bitmap background = null;
	Context context;
	
	public GetFromGallery(Context context){
		this.context = context;
		Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		Log.d("INTENT GALERY  ", "LAUNCHED");
		startActivityForResult(i, SELECT_PICTURE);
	 }
	




	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	        if (resultCode == RESULT_OK) {
	            if (requestCode == SELECT_PICTURE) {
	                Uri selectedImageUri = data.getData();

	                Log.d("INTENT GALERY  ", "RESULT RECEIVED");
	            
						try {
							this.background = MediaStore.Images.Media.getBitmap(context.getContentResolver(), selectedImageUri);
						    Log.d("INTENT GALERY  ", "GOT IMAGE");
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
	            }
	        }
	    }
	
	
	public Bitmap getBG(){
		Log.d("INTENT GALERY  ", "GET BG");
		return this.background;
	}
	
}
